// pages/add/join/join.js
const { $Message } = require('../../../dist/base/index');
var ctx;
function sleep(d) {
  for (var t = Date.now(); Date.now() - t <= d;);
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    groupnum: '',
    name: '加载中...',
    NbrMenbr: 0,
    ifshow: false,
    ifjoin: false,
    ifloding: true,
    actions2: [
      {
        name: '删除',
        color: '#ed3f14'
      }
    ],
    visible: false,
    text: '',//验证码
    showDialog: false,
    checknum:'',//输入的验证码
    OKloading:'确认',
    refuse_close_dialog:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    drawPic(that);
    console.log(options.groupnum)
    wx.request({
      url: 'https://localhost:8080/get_addinfo',
      data: {
        'groupnum': options.groupnum
      },
      success(res) {
        if (res.data.errCode == 2020) {
          that.setData({
            groupnum: options.groupnum,
            name: res.data.name,
            NbrMenbr: res.data.NbrMenbr,
            ifshow: true
          })
          wx.getStorage({
            key: 'pwdWithSalt',
            success(res) {
              wx.request({
                url: 'https://localhost:8080/check_joined',
                data: {
                  groupnum: that.data.groupnum,
                  pwdWithSalt: res.data
                },
                success(res) {
                  if (res.data == '1') {
                    that.setData({
                      ifjoin: true
                    })
                  }
                  else {
                    that.setData({
                      ifjoin: false
                    })
                  }
                }
              })
            }
          })
        }
        else if (res.data.errCode == 2019) {
          wx.showToast({
            title: '该小组不存在',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            ifloding: false
          })
          sleep(2000)
          wx.navigateBack({
            delta: 1
          })
        }
      }
    })
  },

  checknuminput:function(e){
    this.setData({
      checknum:e.detail.value
    })
  },

  Bcancel:function(){
    if(this.data.refuse_close_dialog==false){
      this.setData({
        showDialog:false
      })
    }

  },

  Bok:function(){
    var that =this
    var inputnum=this.data.checknum.toUpperCase ()
    if(this.data.text==inputnum)
    {
      this.setData({
        OKloading:'连接中..',
        refuse_close_dialog:true
      })
      wx.getStorage({
        key: 'pwdWithSalt',
        success(res) {
          wx.request({
            url: 'https://localhost:8080/quit',
            data: {
              groupnum: that.data.groupnum,
              pwdWithSalt: res.data
            },
            success(res) {
              console.log(res.data)
              if (res.data.errCode == 2019) {
                wx.showToast({
                  title: res.data.errmsg,
                  icon: none
                })
                that.setData({
                  showDialog:false,
                  refuse_close_dialog:false,
                  OKloading:'确认'
                })
              }
              else if (res.data.errCode == 2020) {
                wx.navigateBack({
                  delta: 1
                });
                that.setData({
                  showDialog:false,
                  refuse_close_dialog:false,
                  OKloading:'确认'
                })
              }
              else if (res.data.errCode == 3021) {
                that.setData({
                  deltext: res.data.errmsg,
                  visible: true,
                  showDialog:false,
                  refuse_close_dialog:false,
                  OKloading:'确认'
                })
              }
              else if (res.data.errCode == 3022) {
                that.setData({
                  deltext: res.data.errmsg,
                  visible: true,
                  showDialog:false,
                  refuse_close_dialog:false,
                  OKloading:'确认'
                })
              }
            }
          })
        }
      })
    }
    else
    {
      drawPic(that);
      wx.showToast({
        title:'验证码错误',
        icon:'none'
      })
    }
  },

  change: function () {
    var that = this;
    drawPic(that);
  },
  mobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  Bjoin: function () {
    var that = this
    wx.getStorage({
      key: 'pwdWithSalt',
      success(res) {
        wx.request({
          url: 'https://localhost:8080/join',
          data: {
            groupnum: that.data.groupnum,
            pwdWithSalt: res.data
          },
          success(res) {
            if (res.data.errCode == 2019) {
              wx.showToast({
                title: res.data.errmsg,
                icon: none
              })
            }
            else if (res.data.errCode == 2020) {
              wx.navigateBack({
                delta: 1
              });
            }
          }
        })
      }
    })
  },

  Bquit: function () {
    var that = this
    // wx.getStorage({
    //   key: 'pwdWithSalt',
    //   success(res) {
    //     wx.request({
    //       url: 'https://localhost:8080/quit',
    //       data: {
    //         groupnum: that.data.groupnum,
    //         pwdWithSalt: res.data
    //       },
    //       success(res) {
    //         console.log(res.data)
    //         if (res.data.errCode == 2019) {
    //           wx.showToast({
    //             title: res.data.errmsg,
    //             icon: none
    //           })
    //         }
    //         else if (res.data.errCode == 2020) {
    //           wx.navigateBack({
    //             delta: 1
    //           });
    //         }
    //         else if (res.data.errCode == 3021) {
    //           that.setData({
    //             deltext: res.data.errmsg,
    //             visible: true
    //           })
    //         }
    //         else if (res.data.errCode == 3022) {
    //           that.setData({
    //             deltext: res.data.errmsg,
    //             visible: true
    //           })
    //         }
    //       }
    //     })
    //   }
    // })
    drawPic(that);
    this.setData({
      showDialog:true,
      checknum:''
    })
  },

  handleCancel: function () {
    this.setData({
      visible: false
    })
  },

  handleClickItem: function () {
    var that = this
    const action = this.data.actions2
    action[0].loading = true;
    this.setData({
      actions2: action
    })
    wx.getStorage({
      key: 'pwdWithSalt',
      success(res) {
        wx.request({
          url: 'https://localhost:8080/quit_admin',
          data: {
            groupnum: that.data.groupnum,
            pwdWithSalt: res.data
          },
          success(res) {
            if (res.data.errCode == 2020) {
              action[0].loading = false;
              that.setData({
                actions2: action
              })
              that.setData({
                visible: false
              })
              wx.navigateBack({
                delta: 1
              })
            }
            else {
              wx.showToast({
                title: '出现异常',
                icon: 'none'
              })
              that.setData({
                visible: false
              })
            }
          }
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function randomNum(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}
/**生成一个随机色**/
function randomColor(min, max) {
  var r = randomNum(min, max);
  var g = randomNum(min, max);
  var b = randomNum(min, max);
  return "rgb(" + r + "," + g + "," + b + ")";
}

/**绘制验证码图片**/
function drawPic(that) {
  ctx = wx.createCanvasContext('canvas');
  /**绘制背景色**/
  ctx.fillStyle = randomColor(180, 240); //颜色若太深可能导致看不清
  ctx.fillRect(0, 0, 90, 28)
  /**绘制文字**/
  var arr;
  var text = '';
  var str = 'ABCEFGHJKLMNPQRSTWXY123456789';
  for (var i = 0; i < 4; i++) {
    var txt = str[randomNum(0, str.length)];
    ctx.fillStyle = randomColor(50, 160); //随机生成字体颜色
    ctx.font = randomNum(20, 26) + 'px SimHei'; //随机生成字体大小
    var x = 5 + i * 20;
    var y = randomNum(20, 25);
    var deg = randomNum(-20, 20);
    //修改坐标原点和旋转角度
    ctx.translate(x, y);
    ctx.rotate(deg * Math.PI / 180);
    ctx.fillText(txt, 5, 0);
    text = text + txt;
    //恢复坐标原点和旋转角度
    ctx.rotate(-deg * Math.PI / 180);
    ctx.translate(-x, -y);
  }
  /**绘制干扰线**/
  for (var i = 0; i < 4; i++) {
    ctx.strokeStyle = randomColor(40, 180);
    ctx.beginPath();
    ctx.moveTo(randomNum(0, 90), randomNum(0, 28));
    ctx.lineTo(randomNum(0, 90), randomNum(0, 28));
    ctx.stroke();
  }
  /**绘制干扰点**/
  for (var i = 0; i < 20; i++) {
    ctx.fillStyle = randomColor(0, 255);
    ctx.beginPath();
    ctx.arc(randomNum(0, 90), randomNum(0, 28), 1, 0, 2 * Math.PI);
    ctx.fill();
  }
  ctx.draw(false, function () {
    that.setData({
      text: text
    })
  });
}
